package qit.itmiaojie.dao.impl;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import qit.itmiaojie.dao.RoleDao;
import qit.itmiaojie.pojo.Role;

import java.util.List;

public class RoleDaoImpl implements RoleDao {
    /*注入jdbcTemplate*/
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 查询所有角色
     * @return List<Role>
     */
    @Override
    public List<Role> findAll() {
        return jdbcTemplate.query("select * from sys_role",new BeanPropertyRowMapper<Role>(Role.class));
    }

    /**
     * 添加角色
     * @param role
     */
    @Override
    public void addRole(Role role) {
        jdbcTemplate.update("insert into sys_role values (null ,?,?)",role.getRoleName(),role.getRoleDesc());
    }

    @Override
    public List<Role> findRoleByUserId(Long id) {
        List<Role> roleList = jdbcTemplate.query("SELECT * FROM sys_user_role ur,sys_role r WHERE ur.roleId = r.id AND ur.userId = ?", new BeanPropertyRowMapper<>(Role.class),id);
        return roleList;
    }
}
